/*
 * Copyright (c) 2009 Frederic Daoud
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package ${basePackage}.action.admin;

import ${basePackage}.model.Person;

import java.util.List;
import java.util.Map;
import net.sourceforge.stripes.action.ActionBean;
import org.rayures.view.ViewTemplate;
import org.rayures.view.stripes.SLink;

public class PersonTable extends ViewTemplate<PersonTable> {

  private Class<? extends ActionBean> beanclass;

  public PersonTable init(Class<? extends ActionBean> beanclass, List<Person> people) {
    this.beanclass = beanclass;

    put("people", people);

    return this;
  }

  public Object getEditLink(Person person) {
    SLink editLink = tag(SLink.class).beanclass(beanclass).event("edit");
    return editLink.param("person", person.getPersonId()).toString();
  }

  public Object getRemoveLink(Person person) {
    SLink removeLink = tag(SLink.class).beanclass(beanclass).event("remove");
    return removeLink.param("person", person.getPersonId())
      .onclick(getConfirmationMessage(person)).toString();
  }

  private String getConfirmationMessage(Person person) {
    Map<String, String> bundle = getViewContext().getBundle();
    return String.format("return window.confirm('%s %s?');",
      bundle.get("remove"), person.getName());
  }
}
